package com.lzy.mall.product.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.product.dao.PmsAttrGroupDao;
import com.lzy.mall.product.entity.PmsAttr;
import com.lzy.mall.product.entity.PmsAttrGroup;
import com.lzy.mall.product.service.PmsAttrAttrgroupRelationService;
import com.lzy.mall.product.service.PmsAttrGroupService;
import com.lzy.mall.product.service.PmsAttrService;
import com.lzy.mall.product.service.PmsCategoryService;
import com.lzy.mall.product.vo.AttrGroupRelationVo;
import com.lzy.mall.product.vo.AttrGroupWithAttrsVo;
import com.lzy.mall.product.vo.PmsAttrGroupVo;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 属性分组(PmsAttrGroup)表控制层
 *
 * @author lzy
 * @since 2021-08-25 11:41:09
 */
@RestController
@RequestMapping("attrgroup")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsAttrGroupController {

    private final PmsAttrGroupService groupService;

    private final PmsAttrAttrgroupRelationService relationService;

    private final PmsAttrService attrService;

    private final PmsAttrGroupDao attrGroupDao;

    private final PmsCategoryService categoryService;

    @PostMapping("/attr/relation")
    public ResultUtils addRelation(@RequestBody List<AttrGroupRelationVo> vos){

        relationService.saveBatch(vos);
        return ResultUtils.ok();
    }

    /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public ResultUtils getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  groupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return ResultUtils.ok().put("data",vos);
    }

    /**
     * 根据分组id查询所有属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public ResultUtils attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<PmsAttr> entities =  attrService.getRelationAttr(attrgroupId);
        return ResultUtils.ok().put("data",entities);
    }

    /**
     * 获取当前分组没有关联的所有属性
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public ResultUtils attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return ResultUtils.ok().put("page",page);
    }

    /**
     * 列表
     */
    @GetMapping("/list/{cateLogId}")
    public ResultUtils queryPage(@RequestParam Map<String,Object> map,
                                 @PathVariable("cateLogId") Long cateLogId){
        PageUtils page = groupService.queryPage(map,cateLogId);
        return ResultUtils.ok().put("page",page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public ResultUtils info(@PathVariable("attrGroupId") Long attrGroupId){
        PmsAttrGroup attrGroup = attrGroupDao.single(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] path = categoryService.findCatelogPath(catelogId);

        PmsAttrGroupVo vo = new PmsAttrGroupVo();
        BeanUtil.copyProperties(attrGroup,vo);
        vo.setCatelogPath(path);

        return ResultUtils.ok().put("attrGroup", vo);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public ResultUtils save(@RequestBody PmsAttrGroup attrGroup){
        groupService.save(attrGroup);

        return ResultUtils.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public ResultUtils update(@RequestBody PmsAttrGroup attrGroup){
        groupService.updateById(attrGroup);

        return ResultUtils.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResultUtils delete(@RequestBody Long[] attrGroupIds){
        groupService.removeByIds(CollUtil.toList(attrGroupIds));

        return ResultUtils.ok();
    }

    @PostMapping("/attr/relation/delete")
    public ResultUtils deleteRelation(@RequestBody  AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return ResultUtils.ok();
    }
}
